package com.zy.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.mapper.JurisdictionMapper;

import com.zy.mapper.PermissionMapper;
import com.zy.pojo.Jurisdiction;
import com.zy.pojo.Permission;
import com.zy.service.JurisdictionService;
import com.zy.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service("jurisdictionService")
@Slf4j
public class JurisdictionServiceImpl extends ServiceImpl<JurisdictionMapper, Jurisdiction> implements JurisdictionService {
    @Resource
    private JurisdictionMapper jurisdictionMapper;
    @Resource
    private PermissionMapper permissionMapper;


    //特有的查询节点方法
    private List<TreeNode> getnodes(Integer pid,Integer userId){
        List<Integer> perids = permissionMapper.getPerIdByUserId(userId);
        List<TreeNode> nodes = new ArrayList<>();
        List<Jurisdiction> list = getJurisdictions(pid);

        for (Jurisdiction jurisdiction :list){
            TreeNode treeNode = new TreeNode();
            treeNode.setId(jurisdiction.getId());
            treeNode.setTitle(jurisdiction.getName());
            treeNode.setExpand(false);
            //判断是不是最小节点
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("parentid",jurisdiction.getId());
            Integer count = this.baseMapper.selectCount(queryWrapper1);
            if(perids.contains(jurisdiction.getId())&&count==0){
                treeNode.setChecked(true);
            }
            nodes.add(treeNode);
        }
        return nodes;
    }

    private List<Jurisdiction> getJurisdictions(Integer pid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (pid == null){
            queryWrapper.isNull("parentid");
        }else {
            queryWrapper.eq("parentid",pid);
        }

        return super.list(queryWrapper);
    }

    @Override
    public List<TreeNode> getTreeNodes(Integer userId) {
        //一级节点
        List<TreeNode> nodes01 = this.getnodes(null,userId);
        for (TreeNode node01 :nodes01){
            //二级节点
            List<TreeNode> nodes02 = this.getnodes(node01.getId(),userId);
            node01.setChildren(nodes02);
            for (TreeNode node02 :nodes02){
                //三级节点
                List<TreeNode> nodes03 = this.getnodes(node02.getId(),userId);
                node02.setChildren(nodes03);
            }
        }
        return nodes01;
    }

    @Override
    public void dogrant(Integer userId, Integer[] nid) {
        //根据perid删除  中间表
        permissionMapper.deleteById(userId);
        for (Integer perid :nid){
            permissionMapper.insert(new Permission(userId,perid));
        }
    }

    int count =0;
    @Override
    public List<String> getPercodes(Integer cid) {
        log.info("方法getpercodes()------------输出次数:"+(++count));
        return jurisdictionMapper.getPercodes(cid);
    }

    @Override
    public List<Jurisdiction> getPerMenus() {
        Subject subject = SecurityUtils.getSubject();
        //一级菜单栏目
        List<Jurisdiction> menus01 = this.getJurisdictions(null);
        menus01 = menus01.stream().filter(jurisdiction -> subject.isPermitted(jurisdiction.getPermcode())).collect(Collectors.toList());
        for (Jurisdiction j01:menus01){
            //二级菜单栏目
            List<Jurisdiction> menus02 = this.getJurisdictions(j01.getId());
            menus02 = menus02.stream().filter(jurisdiction -> subject.isPermitted(jurisdiction.getPermcode())).collect(Collectors.toList());
            j01.setChildren(menus02);
        }
        return menus01;
    }
}
